package im.vinci.server.common.tmsg;

import im.vinci.server.utils.Networks;

import java.sql.Timestamp;
import java.util.concurrent.atomic.AtomicInteger;

public class TimestampAndHostIdGenerator {
    private static final int[] codex = {2, 3, 5, 6, 8, 9, 19, 11, 12, 14, 15, 17, 18};
    private static final AtomicInteger messageOrder = new AtomicInteger(0);

    private static final String localAddress = Networks.getSiteIp();

    private static final int PID = getPID();

    public static void main(String[] args) {
        System.out.println(getPID());
    }

    public String getNext() {
        StringBuilder sb = new StringBuilder(40);
        long time = System.currentTimeMillis();
        String ts = new Timestamp(time).toString();

        for (int idx : codex)
            sb.append(ts.charAt(idx));
        sb.append('.').append(localAddress);
        sb.append('.').append(PID);
        sb.append('.').append(messageOrder.getAndIncrement());
        return sb.toString();
    }

    private static int getPID() {
        String processName =
                java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
        return Integer.parseInt(processName.split("@")[0]);
    }
}